package com.njbdqn.springajax.ctrl;

import java.io.File;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.njbdqn.springajax.service.NormalService;

@Controller
@Scope("prototype")
public class UploadCtrl {
	@Autowired
	private NormalService normalService;
	
	@RequestMapping("/upload.do")
	public @ResponseBody String upload(@RequestParam("file")MultipartFile file,HttpSession session)throws Exception{
		String path=session.getServletContext().getRealPath("/upload");
		File pfile=new File(path);
		if(!pfile.exists()){
			pfile.mkdirs();
		}
		String newName=normalService.getNewName(file.getOriginalFilename());
		File saveFile=new File(path,newName);
		file.transferTo(saveFile);
		
		return  "{\"imgname\":\""+newName+"\"}";
	}
}
